Page({

  /**
   * 页面的初始数据
   */
  data: {
    title:'国家一级心理咨询师',
    show: false,
    city:'河南省·郑州市',
    actions: [
        {
          name: '国家一级心理咨询师',
        },
        {
          name: '国家二级心理咨询师',
        },
        {
          name: '国家三级心理咨询师',
        },
    ],
    buttonList:[
        "全部","个人成长","恋爱关系","婚姻家庭","亲子教育","职场心理","两性心理","抑郁梳理","情绪管理","人际关系","心理健康","婆媳矛盾"
    ],
    button1:[
        "男","女","不限"
    ],
    button2:[
        "全部方式","视频咨询","语音咨询"
    ],
    xiu: false,
    options:[
        {
            text: '河南省',
            value: '330000',
            children: [
                { text: '郑州市', value: '330100'},
                { text: '信阳市', value: '330200'},
                { text: '新乡市', value: '330300'},
                { text: '濮阳市', value: '330400'},
            ],
        },
        {
            text: '江苏省',
            value: '320000',
            children: [
                { text: '南京市', value: '320100' },
                { text: '无锡市', value: '320200' },
                { text: '苏州市', value: '320300' },
            ],
        },
    ],
    fieldValue: '',
    cascaderValue: '',
    selectedButtonIndexes: [],
    colors: [],
    bacs:[],
    selectBq:[],
    selectedButtonIndexes1: [],
    colors1: [],
    bacs1:[],
    selectSex:'',
    selectedButtonIndexes2: [],
    colors2: [],
    bacs2:[],
    selectMethod:''
  },

  showPopup() {
    this.setData({ show: true });
  },


  onClose() {
    this.setData({ show: false });
    this.setData({xiu:false});
  },

  onSelect(event) {
      this.setData({
          title:event.detail.name
      })
      console.log(this.data.title);
  },

  onClick() {
    this.setData({
      xiu: true
    });
  },

  onFinish(e) {
    // console.log(e);
    const { selectedOptions, value } = e.detail;
    const fieldValue = selectedOptions
        .map((option) => option.text || option.name)
        .join('·');
    this.setData({
      fieldValue,
      cascaderValue: value,
    })
    // console.log(fieldValue);
    console.log(this.data.fieldValue);
  },

  selectButton(e){
    // console.log(e);
    const index = e.currentTarget.dataset.index;
    const biaoqian=e.currentTarget.dataset.item;
    const selectedButtonIndexes = this.data.selectedButtonIndexes;
    const selectBq=this.data.selectBq;
    const newColors = [...this.data.colors];
    const newBacs = [...this.data.bacs];
    if (selectedButtonIndexes.includes(index)) {
      // 如果按钮已被选中，再次点击则取消选中并恢复颜色为白色
      selectedButtonIndexes.splice(selectedButtonIndexes.indexOf(index), 1);
      selectBq.splice(selectBq.indexOf(index),1);
      newColors[index] = '';
      newBacs[index] = '';
      this.setData({
        selectedButtonIndexes,
        colors: newColors,
        bacs:newBacs
      });
    } else {
      // 如果按钮未被选中，点击后设置颜色为蓝色并记录选中状态
      if(selectedButtonIndexes.length<3){
        selectedButtonIndexes.push(index);
        selectBq.push(biaoqian);
        newColors[index] = 'white';
        newBacs[index] = '#87d2f8';
      }else{
          wx.showToast({
            title: '最多选择三个',
          });
          return;
      }
    }
    this.setData({
        selectedButtonIndexes,
        colors: newColors,
        bacs:newBacs,
        selectBq,
    });
    console.log(this.data.selectBq);
  },

  selectButton1(e){
    const index = e.currentTarget.dataset.index;
    const selectedButtonIndexes1 = this.data.selectedButtonIndexes1;
    if (selectedButtonIndexes1.includes(index)) {
      // 如果按钮已被选中，再次点击则取消选中并恢复颜色为白色
      selectedButtonIndexes1.splice(selectedButtonIndexes1.indexOf(index), 1);
      const newColors = this.data.colors1.map(() => '');
      const newBacs=this.data.bacs1.map(()=>'');
      this.setData({
        selectedButtonIndexes1,
        colors1: newColors,
        bacs1:newBacs
      });
    } else {
      // 如果按钮未被选中，点击后设置颜色为蓝色并记录选中状态
      selectedButtonIndexes1.push(index);
      const newColors = this.data.colors1.map(() => '');
      newColors[index] = 'white';
      const newBacs=this.data.bacs1.map(()=>'');
      newBacs[index] = '#87d2f8';
      this.setData({
        selectedButtonIndexes1,
        colors1: newColors,
        bacs1:newBacs,
        selectSex:e.currentTarget.dataset.button
      });
      console.log(this.data.selectSex);
    }
  },
  selectButton2(e){
    const index = e.currentTarget.dataset.index;
    const selectedButtonIndexes2 = this.data.selectedButtonIndexes2;
    if (selectedButtonIndexes2.includes(index)) {
      // 如果按钮已被选中，再次点击则取消选中并恢复颜色为白色
      selectedButtonIndexes2.splice(selectedButtonIndexes2.indexOf(index), 1);
      const newColors = this.data.colors2.map(() => '');
      const newBacs=this.data.bacs2.map(()=>'');
      this.setData({
        selectedButtonIndexes2,
        colors2: newColors,
        bacs2:newBacs
      });
    } else {
      // 如果按钮未被选中，点击后设置颜色为蓝色并记录选中状态
      selectedButtonIndexes2.push(index);
      const newColors = this.data.colors2.map(() => '');
      newColors[index] = 'white';
      const newBacs=this.data.bacs2.map(()=>'');
      newBacs[index] = '#87d2f8';
      this.setData({
        selectedButtonIndexes2,
        colors2: newColors,
        bacs2:newBacs,
        selectMethod:e.currentTarget.dataset.button
      });
      console.log(this.data.selectMethod);
    }
  },
  
  reset(e){
      const title="国家一级心理咨询师";
    this.setData({
        fieldValue:'',
        title:title,
        selectBq:[],
        selectSex:'',
        selectMethod:'',
        selectedButtonIndexes: [],
        colors: [], 
        bacs: [],
        selectedButtonIndexes1: [],
        colors1: [],
        bacs1: [],
        selectedButtonIndexes2: [],
        colors2: [],
        bacs2: []
    })
  },

  confirm(e){
      console.log(e);
      const city=this.data.fieldValue;
      const title=this.data.title;
      const selectTags=this.data.selectBq;
      const selectSex=this.data.selectSex;
      const selectMethod=this.data.selectMethod;
      if(city==''){
          wx.showToast({
            title: '请选择城市',
            icon:'error'
          })
          return;
      }
      if(selectTags==''){
          wx.showToast({
            title: '至少选择一个标签',
            icon:'none',
          });
          return;
      }
      let address = city;
      let gradeName = title;
      let adeptName = selectTags;
      let sex = selectSex;
      let type = selectMethod;
      wx.request({
        url: 'http://localhost:8080/zixunshi/all1',
        method:"GET",
        data:{
            address:address,
            gradeName:gradeName,
            adeptName:adeptName,
            sex:sex,
            type:type,
        },
        success:(res)=>{
            console.log(res);
            let url='?address=' + address + '&gradeName=' + gradeName + '&adeptName=' + adeptName + '&sex=' + sex + '&type=' + type;
            wx.navigateTo({
                url: '/pages/tuijianResult_lfy/tuijianResult_lfy'+url,
                success:function(res){
                    console.log("跳转成功");
                    },fail:function(res){
                    console.log("跳转失败");
                    },complete:function(res){
                    console.log("跳转完成....");
                    }
            })
        },
        fail:(res)=>{
            wx.showToast({
              title: '网络错误',
            })
        }
      })
   
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
  
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})